package org.mspring.mlog.service;

import org.mspring.mlog.dao.StatDao;
import org.mspring.mlog.entity.Stat;
import org.mspring.mlog.entity.constants.CacheNames;
import org.mspring.platform.cache.Cache;
import org.mspring.platform.cache.CacheManager;
import org.mspring.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Gao Youbo
 * @since 2012-8-14
 */
@Service
public class StatService {

    private StatDao statDao;
    private PostService postService;
    private CommentService commentService;
    private CacheManager cacheManager;
    private Cache defaultCache;

    /**
     * 获取文章总数
     *
     * @return
     */
    public int getPostCount() {
        Stat stat = getSingle(Stat.Type.POST_COUNT);
        if (stat != null && StringUtils.isNotBlank(stat.getValue())) {
            return Integer.parseInt(stat.getValue());
        }
        return 0;
    }

    /**
     * 获取评论总数
     *
     * @return
     */
    public int getReplys() {
        Stat stat = getSingle(Stat.Type.COMMENT_COUNT);
        if (stat != null && StringUtils.isNotBlank(stat.getValue())) {
            return Integer.parseInt(stat.getValue());
        }
        return 0;
    }

    /**
     * 获取网站点击率
     *
     * @return
     */
    public int getClickCount() {
        Stat stat = getSingle(Stat.Type.CLICK_COUNT);
        if (stat != null && StringUtils.isNotBlank(stat.getValue())) {
            return Integer.parseInt(stat.getValue());
        }
        return 0;
    }

    /**
     * 更新文章总数量
     */
    public void updatePostCount() {
        long count = postService.countPost();
        Stat stat = new Stat(Stat.Type.POST_COUNT, String.valueOf(count));
        statDao.create(stat);
    }

    /**
     *
     * 更新评论总数量
     */
    public void updateReplys() {
        long count = commentService.countComment();
        Stat stat = new Stat(Stat.Type.COMMENT_COUNT, String.valueOf(count));
        statDao.create(stat);
    }

    /**
     * 更新点击率
     */
    public void updateClickCount() {
        int value = getClickCount() + 1;
        Stat stat = new Stat(Stat.Type.CLICK_COUNT, String.valueOf(value));
        statDao.create(stat);
    }

    private Stat getSingle(final String type) {
        String key = "stat_" + type;
        Stat stat = defaultCache.get(key);
        if (stat == null) {
            stat = statDao.findByType(type);
            defaultCache.set(key, stat, Cache.ONE_MINUTE * 10);
        }
        return stat;
    }

    @Autowired
    public void setStatDao(StatDao statDao) {
        this.statDao = statDao;
    }

    @Autowired
    public void setPostService(PostService postService) {
        this.postService = postService;
    }

    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    @Autowired
    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
        this.defaultCache = this.cacheManager.getCache(CacheNames.DEFAULT_CACHE);
    }

}
